package com.hargasembako.core.controller;

import java.io.IOException;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.hargasembako.core.PMF;
import com.hargasembako.core.entity.Product;
import com.hargasembako.core.entity.ProductPrice;
import com.hargasembako.core.entity.Store;
import com.hargasembako.core.util.SearchJanitor;

@SuppressWarnings("serial")
public class ProductServlet extends HttpServlet {
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
	throws ServletException, IOException {
		doGet(req, resp);
	}
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	
		String action = req.getParameter("action");
		
		if(action.equals("productLookup")) {
			productLookup(req, resp);
		} else if (action.equals("productCreate")){
			productCreate(req, resp);
		} else if (action.equals("productSave")){
			productSave(req, resp);
		} else if (action.equals("productDisplay")){
			productDisplay(req, resp);
		}
	}


	private void productDisplay(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// create the persistence manager instance
		PersistenceManager pm = PMF.get().getPersistenceManager();
		
		// fetch the account
		Key k = KeyFactory.createKey(Product.class.getSimpleName(), 
				new Integer(req.getParameter("productId")).intValue());
		
		Product p = pm.getObjectById(Product.class, k);
		
		// query for the price
		String query = "select from " + ProductPrice.class.getName() + " where productId == " + req.getParameter("productId");
		List<ProductPrice> prices = (List<ProductPrice>) pm.newQuery(query).execute();
		
		// pass the list to the jsp
		req.setAttribute("product", p);
		
		// pass the list to the jsp
		req.setAttribute("prices", prices);
		
		// forward the request to the jsp
		RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/productDisplay.jsp");
		dispatcher.forward(req, resp);
	}

	private void productSave(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		// create the persistence manager instance
		PersistenceManager pm = PMF.get().getPersistenceManager();
		
		// create the new account
		//String code, String name, String unit, String categoryName, String brandName
		Product a = new Product(
			req.getParameter("code"),
			req.getParameter("name"),
			req.getParameter("unit"),
			req.getParameter("category"),
			req.getParameter("brand")
		);
		
		// persist the entity
		try {
			pm.makePersistent(a);
		} finally {
			pm.close();
		}
		resp.sendRedirect("/product?action=productDisplay&productId="+a.getId());	
	}
	
	private void productCreate(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		resp.sendRedirect("/productCreate.jsp");	
	}

	private void productLookup(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// create the persistence manager instance
		PersistenceManager pm = PMF.get().getPersistenceManager();

		// query for the entities by name
		String className = Product.class.getName();
		String queryString = req.getParameter("product");
		
		//String query = "select from " + Store.class.getName() + " where name like '"+req.getParameter("storeName")+"'";
		//List<Store> stores = (List<Store>) pm.newQuery(query).execute();
		
		List<Product> products = SearchJanitor.search(className, queryString, pm);
		
		// pass the list to the jsp
		req.setAttribute("products", products);
		
		// forward the req to the jsp
		RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/productLookup.jsp");
		dispatcher.forward(req, resp);	
	}

}
